package com.sita.tboard.hitchhike;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.AutoCompleteTextView;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.orhanobut.dialogplus.DialogPlus;
import com.orhanobut.dialogplus.OnClickListener;
import com.sita.bike.R;
import com.sita.bike.support.GlobalContext;
import com.sita.bike.utils.PersistUtils;
import com.sita.tboard.ActivityBase;
import com.sita.tboard.hitchhike.event.HitchhikePassengerReloadEvent;
import com.sita.tboard.hitchhike.event.HitchhikePassengerTripPayEvent;
import com.sita.tboard.hitchhike.helper.HitchhikeNetworkHelper;
import com.sita.tboard.hitchhike.helper.HitchhikePersistHelper;
import com.sita.tboard.hitchhike.listeners.OnPayHitchhikeListener;
import com.sita.tboard.hitchhike.running.DriverRunningTripsFragment;
import com.sita.tboard.hitchhike.running.PassengerRunningTripsFragment;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.BindString;
import butterknife.ButterKnife;
import cn.trinea.android.common.util.ToastUtils;
import de.greenrobot.event.EventBus;

/**
 * Created by xiaodong on 16/4/9.
 */
public class HitchhikeActivity extends ActivityBase {

    @Bind(R.id.toolbar)
    Toolbar mToolbar;

    @Bind(R.id.tab_layout)
    CommonTabLayout mTabLayout;

    @BindString(R.string.passenger)
    String mPassenger;
    @BindString(R.string.driver)
    String mDriver;

    private ArrayList<Fragment> mFragments = new ArrayList<Fragment>();
    private ArrayList<CustomTabEntity> mEntities = new ArrayList<>();
    private int navigatePos = 0;

    public static Intent newIntent(Integer tag) {
        Intent intent = new Intent(GlobalContext.getGlobalContext(), HitchhikeActivity.class);
        if (tag != null) {
            intent.putExtra("tag", tag);
        }
        return intent;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent.hasExtra("tag")){
            navigatePos = intent.getIntExtra("tag", 0);
        }
        mTabLayout.setCurrentTab(navigatePos);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hitchhike_main);

        ButterKnife.bind(this);

        setSupportActionBar(mToolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        mToolbar.setNavigationIcon(R.mipmap.ic_back);

        mEntities.add(new TabEntity(mPassenger, R.mipmap.ic_car, R.mipmap.ic_car));
        mEntities.add(new TabEntity(mDriver, R.mipmap.ic_car_2, R.mipmap.ic_car_2));

        mFragments.add(PassengerRunningTripsFragment.newInstance());
        mFragments.add(DriverRunningTripsFragment.newInstance());

        mTabLayout.setTabData(mEntities, this, R.id.fragment_container, mFragments);

        mTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mTabLayout.setCurrentTab(position);
            }

            @Override
            public void onTabReselect(int position) {
                mTabLayout.setCurrentTab(position);
            }
        });

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /*@Override
    protected void onStop() {
        EventBus.getDefault().unregister(this);
        super.onStop();
    }

    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }*/

    class TabEntity implements CustomTabEntity {
        public String title;
        public int selectedIcon;
        public int unSelectedIcon;

        public TabEntity(String title, int selectedIcon, int unSelectedIcon) {
            this.title = title;
            this.selectedIcon = selectedIcon;
            this.unSelectedIcon = unSelectedIcon;
        }

        @Override
        public String getTabTitle() {
            return title;
        }

        @Override
        public int getTabSelectedIcon() {
            return selectedIcon;
        }

        @Override
        public int getTabUnselectedIcon() {
            return unSelectedIcon;
        }
    }
}
